package aop.recipe.common;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import model.recipe.RecipeVO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import aop.recipe.model.ReportService;
import aop.recipe.model.ReportVO;

@Aspect
public class ReportAspect {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name = "reportService")
	private ReportService reportService;

	public ReportService getReportService() {
		return reportService;
	}

	@Around("execution(public * model.recipe.*Service.getContent(..))")
	public Object clickCheckLogging(ProceedingJoinPoint point) throws Throwable {
		String className = point.getTarget().getClass().getName();
		String methodName = point.getSignature().getName();
		Object params[] = point.getArgs();
		Object retValue = point.proceed();
		if (params.length > 0)
			log.info(className + " " + methodName + " " + params[0]);
		else
			log.debug("aop test");
		HashMap map = (HashMap) retValue;
		RecipeVO rvo = (RecipeVO) map.get("rvo");
		String user_id = (String) map.get("user_id");
		String content_id = (String) map.get("content_id");
		boolean flag = (Boolean) map.get("flag");
		HashMap hmap = new HashMap();
		hmap.put("r_no", rvo.getR_no());
		hmap.put("re_type", "click");
		int count = reportService.RankingRecipeCheckByNo(hmap);
		if (user_id != null) {
			if (!user_id.equals(content_id)) {
				if (count == 0) {
					ReportVO revo = new ReportVO(rvo.getR_title(),
							rvo.getR_no(), 1, null, "click");
					reportService.addRankingRecipe(revo);
				} else {
					reportService.updateRankingRecipeCount(rvo.getR_no());
				}
			}
		} else if (flag == true) {
			if (count == 0) {
				ReportVO revo = new ReportVO(rvo.getR_title(), rvo.getR_no(),
						1, null, "click");
				reportService.addRankingRecipe(revo);
			} else {
				reportService.updateRankingRecipeCount(rvo.getR_no());
			}
		}

		log.info(className + " " + methodName);
		System.out.println("aop");
		return retValue;
	}

	@Around("execution(public * model.recipe.*Service.searchByRecipe(..))")
	public Object searchCheckLogging(ProceedingJoinPoint point)
			throws Throwable {
		String className = point.getTarget().getClass().getName();
		String methodName = point.getSignature().getName();
		Object params[] = point.getArgs();
		Object retValue = point.proceed();
		HashMap map = (HashMap) params[0];
		String word = (String) map.get("s_word");
		//검색어가 ""빈공란이 아닐경우 -> NullPointException처리
		if (word != "") {
			System.out.println(retValue+"ddd");
			HashMap ret_map=(HashMap)retValue;
			List list_user=(List) ret_map.get("list_user");
			List list_admin=(List)ret_map.get("list_admin");
			System.out.println(list_user);
			System.out.println(list_admin);
			//검색이 되었을 경우만 검색어 키워드가 추가된다.
			if ( list_user.size()>0||list_admin.size()>0) {
				HashMap hmap = new HashMap();
				hmap.put("re_type", "word");
				hmap.put("r_title", word);

				System.out.println("map" + map);
				// System.out.println("sword"+map.get("s_word"));
				int count = reportService.rankingRecipeCheckByWord(hmap);
				if (count == 0) {
					ReportVO revo = new ReportVO(word, 0, 1, null, "word");
					reportService.addRankingRecipe(revo);
				} else {
					reportService.updateRankingRecipeCountByWord(hmap);
				}
			}
		}
		System.out.println("aop");
		return retValue;
	}

}
